package com.jjmmbb.kebiao;

import java.util.ArrayList;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableLayout.LayoutParams;
import android.widget.TableRow;
import android.widget.TextView;

import com.jjmmbb.kebiao.comm.Comm;
import com.jjmmbb.kebiao.comm.InfoDialog;
import com.jjmmbb.kebiao.comm.Util;
import com.jjmmbb.kebiao.db.CourseDao;
import com.jjmmbb.kebiao.pojo.Course;

public class KeBiaoActivity extends BaseActivity implements OnClickListener {
	TableLayout tableView;

	LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
			LayoutParams.MATCH_PARENT);

	TextView setView, week6View, week7View, versionView, rateView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Util.getPreferences(this);
		setContentView(R.layout.main);

		tableView = (TableLayout) findViewById(R.id.main_table);
		setView = (TextView) findViewById(R.id.main_set);
		week6View = (TextView) findViewById(R.id.main_week6);
		week7View = (TextView) findViewById(R.id.main_week7);
		versionView = (TextView) findViewById(R.id.app_version);
		rateView = (TextView) findViewById(R.id.app_rate);

		setView.setOnClickListener(this);
		rateView.setOnClickListener(this);
		ini();

		// // 布局
		// FrameLayout.LayoutParams lp_Top = new FrameLayout.LayoutParams(
		// FrameLayout.LayoutParams.MATCH_PARENT,
		// FrameLayout.LayoutParams.WRAP_CONTENT);
		//
		// // 设置广告出现的位置(悬浮于顶部)
		// lp_Top.gravity = Gravity.BOTTOM;
		// addContentView(new CustomGifView(this), lp_Top);

		// GifView gf1 = (GifView) findViewById(R.id.gif1);
		// // 设置Gif图片源
		// gf1.setGifImage(R.drawable.wks);
		// gf1.setGifImageType(GifImageType.WAIT_FINISH);

		WebView webview = (WebView) findViewById(R.id.webViewGif);
		webview.loadUrl("file:///android_asset/gif.html");
	}

	void ini() {
		lp.setMargins(1, 1, 1, 1);

		// 版本
		String ver = "1.0";
		try {
			ver = getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName;
		} catch (NameNotFoundException e) {
		}
		versionView.setText(getStr(R.string.app_name) + " v" + ver);

		// 配置
		if (!Comm.need6) {
			week6View.setVisibility(View.GONE);
			week7View.setVisibility(View.GONE);
		}
		if (!Comm.need7) {
			week7View.setVisibility(View.GONE);
		}

		// 早上的数据存储在20~24,上午:0~9,下午10~19,晚上25~29
		// 获取第i节的数据,早上
		for (int i = 20; i < 20 + Comm.coutMorning; i++) {
			addRow(i);
		}

		// 添加间隔
		if (Comm.coutAm > 0 && Comm.coutMorning>0) {
			addRowSplit();
		}

		// 获取第i节的数据,上午
		for (int i = 0; i < Comm.coutAm; i++) {
			addRow(i);
		}

		// 添加间隔
		if (Comm.coutPm > 0) {
			addRowSplit();
		}

		// 获取第i节的数据,下午
		for (int i = 10; i < 10 + Comm.coutPm; i++) {
			addRow(i);
		}

		// 添加间隔
		if (Comm.coutNight > 0) {
			addRowSplit();
		}

		// 获取第i节的数据,晚上
		for (int i = 25; i < 25 + Comm.coutNight; i++) {
			addRow(i);
		}

	}

	@Override
	protected void onResume() {
		// 配置
		if (Comm.isLANDSCAPE) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		} else {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
		}
		super.onResume();
	}

	void addRowSplit() {
		TableRow row = new TableRow(this);
		row.setBackgroundResource(R.color.CTitle);
		TextView x0 = new TextView(this);
		x0.setText(" ");
		x0.setTextSize(5);
		x0.setBackgroundResource(R.color.CTitle);
		row.addView(x0);
		tableView.addView(row);
	}

	void addRow(int n) {
		CourseDao cm = new CourseDao(this);
		ArrayList<Course> rowList;
		rowList = cm.getListByY(n);

		TableRow row = new TableRow(this);

		for (int i = 0; i < rowList.size(); i++) {
			System.out.println(i);
			if (i > 8) {
				break;
			} else if (i >= 6 && !Comm.need6) {
				break;
			} else if (i >= 7 && !Comm.need7) {
				break;
			}
			Course c = rowList.get(i);
			if (c.x == 0) {
				if (n < 10) {
					c.name = "上" + (c.y % 10 + 1) + "节";
				} else if (n < 20) {
					c.name = "下" + (c.y % 10 + 1) + "节";
				} else if (n < 25) {
					c.name = "早" + (c.y % 10 + 1) + "节";
				} else {
					c.name = "晚" + (c.y % 25 + 1) + "节";
				}
			}
			row.addView(addCell(c));
		}

		tableView.addView(row);
	}

	LinearLayout addCell(final Course c) {
		int dp2 = Util.dip2px(this, 2);
		LinearLayout x3 = new LinearLayout(this);
		final TextView x0 = new TextView(this);
		x0.setText(c.name);
		x0.setTextAppearance(this, R.style.textBlue);
		x0.setLayoutParams(lp);
		x0.setPadding(dp2, dp2, dp2, dp2);
		x0.setGravity(Gravity.CENTER);
		if (c.x == 0) {
			x0.setBackgroundResource(R.color.CTitle);
			// x0.setOnClickListener(new OnClickListener() {
			// public void onClick(View arg0) {
			// c.name = "set";
			// x0.setText(c.name);
			// }
			// });
		} else {
			x0.setBackgroundResource(R.color.white);
			x0.setOnClickListener(new OnClickListener() {
				public void onClick(View arg0) {
					if (null != Comm.currentV) {
						Comm.currentV.setBackgroundResource(R.color.white);
					}
					x0.setBackgroundResource(R.color.CTitle);
					Comm.currentC = c;
					Comm.currentV = x0;
					if (Comm.isEdit) {
						InfoDialog dialog = new InfoDialog(KeBiaoActivity.this,
								c, x0);
						dialog.show();
					}
				}
			});

			x0.setOnLongClickListener(new OnLongClickListener() {
				public boolean onLongClick(View arg0) {
					if (null != Comm.currentV) {
						Comm.currentV.setBackgroundResource(R.color.white);
					}
					x0.setBackgroundResource(R.color.CTitle);
					Comm.currentC = c;
					Comm.currentV = x0;
					InfoDialog dialog = new InfoDialog(KeBiaoActivity.this, c,
							x0);
					dialog.show();
					return false;
				}
			});
		}
		x3.addView(x0);
		return x3;
	}

	// // 选择操作
	// PopupWindow selectPopupWindow;
	//
	// void popWindowIni() {
	// LinearLayout popList = new LinearLayout(this);
	// LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
	// LayoutParams.WRAP_CONTENT);
	// popList.setOrientation(LinearLayout.VERTICAL);
	// popList.setLayoutParams(lp);
	//
	// final TextView editView = new TextView(this);
	// final TextView optionView = new TextView(this);
	//
	// int dp8 = Util.dip2px(this, 8);
	//
	// editView.setTextAppearance(this, R.style.MediumBlack);
	// editView.setText(res.getString(R.string.des_edit));
	// editView.setBackgroundResource(R.drawable.btn_a);
	// editView.setPadding(dp8, dp8, dp8, dp8);
	// editView.setOnClickListener(new OnClickListener() {
	// public void onClick(View v) {
	// if (Comm.isEdit) {
	// editView.setText(res.getString(R.string.des_edit));
	// } else {
	// editView.setText(res.getString(R.string.des_read));
	// }
	// Comm.isEdit = !Comm.isEdit;
	// popWindowDismiss();
	// }
	// });
	//
	// optionView.setTextAppearance(this, R.style.MediumBlack);
	// optionView.setText(res.getString(R.string.des_option));
	// optionView.setBackgroundResource(R.drawable.btn_a);
	// optionView.setPadding(dp8, dp8, dp8, dp8);
	// optionView.setOnClickListener(new OnClickListener() {
	// public void onClick(View v) {
	// gotoActivity(OptionActivity.class);
	// popWindowDismiss();
	// }
	// });
	//
	// popList.addView(editView);
	// popList.addView(optionView);
	//
	// selectPopupWindow = new PopupWindow(popList, LayoutParams.WRAP_CONTENT,
	// LayoutParams.WRAP_CONTENT, true);
	//
	// selectPopupWindow.setOutsideTouchable(true);
	// selectPopupWindow.setBackgroundDrawable(new BitmapDrawable());//
	// 加了这句,才能使popWindow消失
	// }
	//
	// void popWindowShow() {
	// if (null == selectPopupWindow) {
	// popWindowIni();
	// }
	// selectPopupWindow.showAsDropDown(setView);
	// }
	//
	// public void popWindowDismiss() {
	// selectPopupWindow.dismiss();
	// }

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.main_set:
			// popWindowShow();
			gotoActivity(OptionActivity.class);
			// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
			break;
		case R.id.app_rate:
			// 显示某个应用的相关信息
			Uri uri = Uri.parse("market://details?id=com.jjmmbb.kebiao");
			Intent it = new Intent(Intent.ACTION_VIEW, uri);
			startActivity(it);
			break;
		default:
			break;
		}
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			// land do nothing is ok
			// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
			System.out.println("lan");
		} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
			// port do nothing is ok
			// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

			System.out.println("por");
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_MENU:
			gotoActivity(OptionActivity.class);
			break;
		case KeyEvent.KEYCODE_BACK:
			this.finish();
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

}